<template>
    <div class="userSearch">
        <a-form-item
            :validate-status="error"
            :help="errorTxt"
        >
            <a-input-search :disabled="disabled" v-model="stdNo" placeholder="输入学工号" :loading="loading" @search="onSearch" enter-button/>
        </a-form-item>
        <a-table
            v-if="showInfo&&dataSource.length"
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false">
          </a-table>
    </div>
  
</template>

<script>
import { postAction, getAction } from '@/api/manage'
export default {
    name:'XSearchUser',
    props:{
        value:[String,Number],
        showInfo:{
            type:Boolean,
            default:()=>{
                return true
            }
        },
        disabled:Boolean
    },
    data(){
        return {
            loading:false,
            stdNo:'',
            error:'',
            errorTxt:'',
            columns:[
                {
                    title: '名称',
                    align:"center",
                    dataIndex: 'name'
                },
                {
                    title: '学院',
                    align:"center",
                    dataIndex: 'college'
                },
                {
                    title: '专业',
                    align:"center",
                    dataIndex: 'major'
                },
                {
                    title: '年级',
                    align:"center",
                    dataIndex: 'grade'
                },
                {
                    title: '班级',
                    align:"center",
                    dataIndex: 'clazz'
                }
            ],
            dataSource:[],
            url:{
                user:'/student/queryByStdNo'
            }
        }
    },
    watch:{
        value(nv,ov){
            this.stdNo = nv
            if(nv){
                this.onSearch(nv)
            }else{
                this.dataSource = []
            }
        }
    },
    methods:{
        onSearch(value,event){
            if(value=='') return
            this.loading = true
            getAction(this.url.user, {
                stdNo: value
            }).then(res => {
                if (res.success && res.result) {
                    let {stdNo,name,clazz,college,major,grade} = res.result
                    this.dataSource = [{stdNo,name,clazz,college,major,grade}]
                    this.error = '';
                    this.errorTxt = '';
                } else {
                    this.dataSource = [];
                    this.error = 'error';
                    this.errorTxt = res.message;
                }
                this.$emit('onchange',this.dataSource[0])
                this.loading = false
            })
        }
    }
}
</script>

<style scoped>
.userSearch{

}
</style>